package day8.homework;

import java.text.DecimalFormat;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//a.一球从100米高度自由落下，每次落地后反跳回原高度的一半；
// 再落下，求它在第10次落地时，共经过多少米？
// 第10次反弹多高？  通过线程模拟这个过程
public class A implements Callable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        FutureTask<double[]> ft = new FutureTask<>(() -> {
            double[] result = new double[2];
            result[0] = 100;
            result[1] = 100;
            for (int i = 1; i < 10; i++) {
                result[1] /= 2;
                result[0] += result[1] * 2;
            }

            return result;
        });
        new Thread(ft).start();
        double[] result = ft.get();
        //DecimalFormat df = new DecimalFormat("#.##");
        //System.out.println("共经过" + df.format(result[0]) + "米");
        //System.out.println("第十次反弹" + df.format(result[1]) + "米");

        System.out.println("共经过" + result[0] + "米");
        System.out.println("第十次反弹" + result[1] + "米");

        //2
//        DemoA test = new DemoA();
//        FutureTask<Double> ft = new FutureTask<Double>(test);
//        Thread thread = new Thread(ft);
//        thread.start();
//        System.out.println(ft.get());

    }

    @Override
    public String call() throws Exception {
        double height = 100;
        double sum = 0;
        for (int i = 1; i <= 10; i++) {
            sum += height;
            height = height / 2;
            sum += height;
            //System.out.println("第" + i + "次落地的高度为:" + height + "\t" + "共经过" + sum + "米");
        }
        return "第10次落地的高度为:" + height + "\t" + "共经过" + sum + "米";
    }
}
